
import os
import shutil
import subprocess
import time
import zipfile


class Sun_os:
    def __init__(self):
        pass


    # 打开一个应用程序  传入程序exe路径
    def open_procedure(self,procedure_path):
        subprocess.Popen([procedure_path])

    # 关闭一个应用程序   传入exe程序名
    def close_procedure(self,procedure_name):
        subprocess.Popen(["taskkill","/f","/im",procedure_name])


    # 执行dos命令 从结果中查找是否存在符合预期字符  响应短
    def execute_short_command_find_expect_str(self,command,expect_str):
        result = subprocess.Popen(command,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,encoding='gbk',shell=True)
        print(result.communicate()[0])
        if expect_str in result.communicate()[0]:
            print(f"{expect_str}在执行结果中")
            return True
        else:
            print(f"{expect_str}不在执行结果中")
            return False

    # 执行dos命令 短命令
    def execute_short_command(self,command):
        result = subprocess.Popen(command,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,encoding='gbk',shell=True)
        return result.communicate()[0]

    # 执行dos命令 长命令
    def execute_long_command(self,command):
        result = subprocess.Popen(command,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,encoding='gbk',shell=True)
        for data in result.stdout:
            print(data)
            time.sleep(2)


    # 执行dos命令 从结果中查找是否存在符合预期字符  响应长
    def execute_long_command_find_expect_str(self,command,expect_str,execute_time=20):
        result = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,encoding='gbk',shell=True)
        start_time = time.time()
        for data in result.stdout:
            if time.time() - execute_time < start_time :
                print(data)
                if expect_str in data:
                    print(f"{expect_str}在执行结果中")
                    return True
                else:
                    print(f"{expect_str}不在执行结果中")
                time.sleep(2)
            else:
                result.terminate()
        print(f"最大执行时间{execute_time}秒到了，停止执行")
        return False

    # 获取一个路径文件的路径部分 绝对路径
    def get_dirname_in_path(self,path_file):
        return os.path.dirname(path_file)

    # 获取一个路径文件的最后部分 带文件的路径就返回文件名 不带文件的路径就返回最后一层目录名
    def get_filename_in_path(self,path_file):
        return os.path.basename(path_file)

    # 获取一个路径文件的后缀名部分
    def get_fileext_in_path(self,path_file):
        if self.judge_path_is_file(path_file):
            return os.path.splitext(path_file)[1]
        else:
            print(f"传入的参数{path_file}不是文件名")

    # 判断一个路径是否为目录
    def judge_path_is_dir(self,path_file):
        return os.path.isdir(path_file)

    # 检查目录或文件是否存在
    def check_dirfile_is_exist(self,dir_path):
        return os.path.exists(dir_path)

    # 判断一个路径是否为文件
    def judge_path_is_file(self,path_file):
        return os.path.isfile(path_file)

    # 两个路径合并成新路径
    def merge_path(self,path,other_path):
        return os.path.join(path,other_path)

    # 创建文件 文件存在会覆盖 不存在会创建
    def make_file(self,path_file):
        open(path_file,"w+")

    # 删除文件
    def delete_file(self,path_file):
        os.remove(path_file)

    # 复制文件
    def copy_file(self,current_file_path,target_file_path):
        shutil.copyfile(current_file_path, target_file_path)

    # 剪切文件
    def move_file(self,current_file_path,target_file_path):
        shutil.move(current_file_path, target_file_path)

    # 获取文件大小
    def get_file_size(self,file_path):
        return os.path.getsize(file_path)

    # 创建目录
    def make_dir(self,dir_path):
        os.mkdir(dir_path)

    # 删除目录
    def delete_dir(self,dir_path):
        shutil.rmtree(dir_path)

    # 复制目录
    def copy_dir(self,current_dir_path,target_dir_name):
        if not self.check_dirfile_is_exist(target_dir_name):
            shutil.copytree(current_dir_path, target_dir_name)
        else:
            print(f"目标{target_dir_name}目录，已经存在了")

    # 剪切目录
    def move_dir(self,current_dir_path,target_dir_name):
        shutil.move(current_dir_path, target_dir_name)

    # 获取文件或目录当前的 读 写 执行 权限
    def check_access(self,path_file):
        return os.access(path_file,os.R_OK),os.access(path_file,os.W_OK),os.access(path_file,os.X_OK)

    # 解压文件
    def unzip_file(self,zip_file_path,unzip_path,unzip_method="zip"):
        if unzip_method == "zip":
            with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
                zip_ref.extractall(unzip_path)

    # 获取一个目录下的所有文件及目录
    def get_dir_data(self,dir_path):
        return os.listdir(dir_path)

    # 获取当前时间 年月日 时分秒
    def get_current_time(self):
        return time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime(time.time()))







